<?php
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011 Alexandre Vaillancourt                                  //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
////////////////////////////////////////////////////////////////////////////////


require_once("wikibot.classes.php");
require_once("function.get_site_stats.data.php");

function get_site_stats() {

	global $get_site_stats_data_site_list;
	
	$sites = explode("\r\n", $get_site_stats_data_site_list);
	array_walk($sites, create_function('&$val', '$val = trim($val);'));
	
	$wpapi = new wikipediaapi();

	$sites_info = array();
	
	foreach($sites as $site)
	{
		$sites_info[] = $wpapi->siteinfo($site . 'api.php', true);
	}
	
	return $sites_info;
}

function format_site_stats_as_wiki_table($sites_info)
{
	$header = <<< EOF
{| class="sortable wikitable" style="text-align:center"
|+'''Know Rappelz Wikis at Wikia.com'''
! Site name
! # of articles
! # of active users
! # of admins
! # of images
EOF;

	$footer = "|}";
	
	$content = "\n";

	// number of users is irrelevent
	foreach($sites_info as $site_info)
	{
		$content .= "|- \n" ;
		$content .= '| [' . $site_info['general']['base'] . ' ' . $site_info['general']['sitename'] . "]\n"; // Site name + url/link
		$content .= '| ' . number_format($site_info['statistics']['articles']) . "\n"; // articles
		$content .= '| ' . number_format($site_info['statistics']['activeusers']) ."\n"; // active users
		$content .= '| ' . number_format($site_info['statistics']['admins']) . "\n"; // admins
		$content .= '| ' . number_format($site_info['statistics']['images']) . "\n"; // images
	}
	
	return $header . $content . $footer;
}



?>